package com.fortune.app.activities;

import android.os.Bundle;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import com.fortune.app.R;
import com.fortune.app.fragments.HomeFragment;
import com.fortune.app.fragments.DivinationFragment;
import com.fortune.app.fragments.DreamFragment;
import com.fortune.app.fragments.ProfileFragment;

/**
 * 应用主活动，负责管理底部导航和主要Fragment
 */
public class MainActivity extends AppCompatActivity {
    private BottomNavigationView bottomNavigationView;
    private FragmentManager fragmentManager;
    
    // 主要Fragment
    private HomeFragment homeFragment;
    private DivinationFragment divinationFragment;
    private DreamFragment dreamFragment;
    private ProfileFragment profileFragment;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // 初始化Fragment
        homeFragment = new HomeFragment();
        divinationFragment = new DivinationFragment();
        dreamFragment = new DreamFragment();
        profileFragment = new ProfileFragment();
        
        // 设置底部导航
        bottomNavigationView = findViewById(R.id.bottom_navigation);
        fragmentManager = getSupportFragmentManager();
        
        // 默认显示首页
        fragmentManager.beginTransaction()
                .add(R.id.fragment_container, homeFragment)
                .commit();
        
        // 设置导航监听
        bottomNavigationView.setOnNavigationItemSelectedListener(
                new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        Fragment selectedFragment = null;
                        
                        switch (item.getItemId()) {
                            case R.id.nav_home:
                                selectedFragment = homeFragment;
                                break;
                            case R.id.nav_divination:
                                selectedFragment = divinationFragment;
                                break;
                            case R.id.nav_dream:
                                selectedFragment = dreamFragment;
                                break;
                            case R.id.nav_profile:
                                selectedFragment = profileFragment;
                                break;
                        }
                        
                        if (selectedFragment != null) {
                            fragmentManager.beginTransaction()
                                    .replace(R.id.fragment_container, selectedFragment)
                                    .commit();
                            return true;
                        }
                        
                        return false;
                    }
                });
    }
}
